package com.tzbank.wisdom.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.tzbank.esb.util.EsbConsumerUtil;
import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.ResultCode;
import com.tzbank.utils.ResultUtil;
import com.tzbank.wisdom.service.CommonService;
import com.tzbank.wisdom.service.OtherCardRegisterService;
import com.tzbank.wisdom.utils.RequestMap;
import com.tzbank.wisdom.utils.TransCodeEnum;

import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;

@Service
@Slf4j
public class OtherCardRegisterServiceImpl implements OtherCardRegisterService {
    @Resource
    private CommonService mCSendMsgToMBService;

    @Override
    public Map<String, Object> CardPreCheck(Map<String, String> reqBody) {
        Map<String, Object> resultMap = otherCardPreCheck(reqBody);
        return resultMap;
    }

    @Override
    public Map<String, Object> otherCardReg(Map<String, String> reqBody) {
        Map<String, Object> cardPreCheckMap = otherCardPreCheck(reqBody);
        String code = (String) cardPreCheckMap.get("code");
        if (code.equals("200")) {
            log.info("请求【{}】", JSONObject.fromObject(reqBody));
            Map<String, String> dataMap = (Map<String, String>) cardPreCheckMap.get("data");
            Map<String, Object> inputMap = new HashMap<String, Object>();
            Map<String, Object> reqSysHead1 = RequestMap.getReqSysHead(TransCodeEnum.OTHER_BANK_REGITER.getServiceCode(), TransCodeEnum.OTHER_BANK_REGITER.getServiceSence(), "", "0110", "");
            inputMap.put("sysHead", reqSysHead1);
            inputMap.put("cardNo", reqBody.get("cardNo"));//注册卡号6214830168652112
            inputMap.put("clientName", reqBody.get("clientName"));//客户名称杨摩星
            inputMap.put("mobile", reqBody.get("mobileNo"));//移动号码15201240620
            inputMap.put("globalId", reqBody.get("globalId"));//证件号码410750195310238454
            inputMap.put("passwordConf", reqBody.get("passwordConf"));//交易密码确认
            inputMap.put("tranPasswordConf", reqBody.get("tranPasswordConf"));//密码确认
            inputMap.put("password", reqBody.get("password"));//交易密码
            inputMap.put("tranPassword", reqBody.get("tranPassword"));//密码
            inputMap.put("relateAcctBankId", reqBody.get("relateAcctBankId"));//关联账户行号
            inputMap.put("bankSerNo", dataMap.get("bankSerNo"));//246
            Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);
            log.info("响应【{}】", JSONObject.fromObject(result));
            if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
                return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
                        result.get("data"));
            }
            return ResultUtil.resultMap("他行卡注册成功", ResultCode.RESULT_CODE_SUCCESS.code(), result.get("data"));
        } else {
            return cardPreCheckMap;
        }
    }

    @Override
    public Map<String, Object> sendAuthCode(Map<String, String> reqBody) {
        reqBody.put("mobile", reqBody.get("mobileNo"));
        reqBody.put("smsInfo", "您正在进行注册身份校验");
        return mCSendMsgToMBService.sendMsgToMB(reqBody);
    }

    private static Map<String, Object> otherCardPreCheck(Map<String, String> reqBody) {
        Map<String, Object> inputMap = new HashMap<String, Object>();
        Map<String, Object> reqSysHead1 = RequestMap.getReqSysHead(TransCodeEnum.TZB_REGISTER_QUERY.getServiceCode(), TransCodeEnum.TZB_REGISTER_QUERY.getServiceSence(), "", "0110", "");
        reqSysHead1.put("bankSystemType", "04590501");
        inputMap.put("sysHead", reqSysHead1);
        inputMap.put("cardNo", reqBody.get("cardNo"));//注册卡号6214830168652112
        inputMap.put("clientName", reqBody.get("clientName"));//客户名称杨摩星
        inputMap.put("mobileNo", reqBody.get("mobileNo"));//移动号码15201240620
        inputMap.put("globalId", reqBody.get("globalId"));//证件号码410750195310238454
        inputMap.put("oppenAcctType", reqBody.get("oppenAcctType"));//开户方式1
        inputMap.put("openBranch", reqBody.get("openBranch"));//开户机构9999
        inputMap.put("sendDate", new Date() + "");
        inputMap.put("bankNo", reqBody.get("bankNo"));//银行代码04590501
        inputMap.put("agentSeqNo", "1111");//中间业务平台流水号1111
        inputMap.put("msgVerCode", "1111");//短信验证码reqBody.get("msgVerCode")12341156
        inputMap.put("othBankName", "1111");//对方行名台州银行
        inputMap.put("checkBranch", "1111");//核查机构0112
        Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);

        if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
            return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
                    result.get("data"));
        }
        return ResultUtil.resultMap("他行卡前置查询 ", ResultCode.RESULT_CODE_SUCCESS.code(), result.get("data"));
    }

}
